package com.liuwei.spring.cloud.junit.leetcode;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class LeetCode {
    public static void main(String[] args) {
        LeetCode code = new LeetCode();
        //code.twoSum();
        int[] heigth = new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
        int water = code.trap(heigth);
        log.info("雨水:{}",water);
    }

    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        Map<String, List<Integer>> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            int num = nums[i];
            String key = String.valueOf(num);
            if(map.get(key) == null){
                List<Integer> values = new ArrayList<>();
                values.add(i);
            }else{
                map.get(key).add(i);
            }
        }

        for(int i=0;i<nums.length;i++){
            int one = nums[i];
            int two = target - one;
            String twoKey = String.valueOf(two).toString();
            List<Integer> twoValue = map.get(twoKey);

            if(map.containsKey(twoKey)){
                if(twoValue.contains(i)){
                    if(twoValue.size()>1){
                        result[0] = twoValue.get(0);
                        result[1] = twoValue.get(1);
                        break;
                    }
                }else{
                    result[0] = i;
                    result[1] = twoValue.get(0);
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 接雨水
     * @param height
     * @return
     */
    public int trap(int[] height) {
        int result = 0;
        int count = 0;
        for(int i=0;i<height.length;i++) {
            int leftMax = findMaxValue(height, 0, i - 1);
            int rightMax = findMaxValue(height, i + 1, height.length - 1);
            if (leftMax <= height[i] || rightMax <= height[i]) {
                // 无法存水
            }else{
                int water = Math.min(leftMax,rightMax) - height[i];
                count = count + water;
            }
        }
        return count;
    }

    private int findMaxValue(int[] source,int startIndex,int endIndex){
        if(startIndex<=0){
            startIndex = 0;
        }
        if(startIndex>=source.length-1){
            startIndex = source.length-1;
        }
        int max = 0 ;
        if(source!=null && source.length>0){
            for (int i = startIndex; i <= endIndex; i++) {
                if(source[i]>max){
                    max = source[i];
                }
            }
        }
        return max;
    }

    public int coinChange(int[] coins, int amount) {
        int min = -1;
        for (int i = 0; i < coins.length; i++) {
            int value = coins[i];

        }
        return min;
    }
}
